package test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiang.MybatisMapper.Mapper;
import com.jiang.pojo.User;
import com.jiang.utils.MyBatisUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package test
 * @Description:
 * @Date 2022/5/31 21:12
 */
public class test {
@Test
    public  void  findUserAll(){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        List<User> userAll = mapper.findUserAll();
        System.out.println(userAll);
    }
    @Test
    public void  findUserlike(){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        List<User> userLike = mapper.findUserLike("张");
        System.out.println(userLike);
    }
    @Test
    public void findUserADD(){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        User user = new User();
        user.setName("李");
        user.setPassword("123");
        user.setSex("男");
        user.setRegistTime(new Date());
        user.setBirthday(new Date());
        mapper.findUserAdd(user);
        MyBatisUtils.commit();
    }
@Test
    public void update (){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        User user = new User();
        user.setId(10);
        user.setName("李丽丽");
        user.setSex("男");
        user.setPassword("123");
        user.setBirthday(new Date());
        user.setRegistTime(new Date());
        mapper.update(user);
        MyBatisUtils.commit();
    }
    @Test
    public void delete (){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        mapper.delete(12);
        MyBatisUtils.commit();
    }

    /**
     * 一对一的关系
     */
    @Test
    public  void  findUserAndUSerDesc(){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        List<User> userAll = mapper.findUserAndUserDesc();
        System.out.println(userAll);
    }


    /**
     * 一对多的关系
     */
    @Test
    public  void  findUserAndOrder(){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        List<User> userAll = mapper.findUserAndOrder();
        System.out.println(userAll);
    }

    /**
     * 多对多的关系
     */
    @Test
    public  void  findUserAndOrderAndProduct(){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        List<User> userAll = mapper.findUserAndOrderAndProduct();
        System.out.println(userAll);
    }

    /**
     * 使用标签模糊查询
     */
    @Test
    public void  FindUserlikenameAndSex(){
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        List<User> likeNameAndSex = mapper.findUserLikeNameAndSex("张",null);
        System.out.println("================"+likeNameAndSex);
    }

    /**
     * 使用set标签修改  不赋值的话就不修改
     */
    @Test
    public void FindUserUpdate() {
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        User user = new User();
        user.setName("姜子牙");
        user.setPassword("123");
        user.setId(10);
        mapper.FindUserUpdate(user);
        MyBatisUtils.commit();
    }
    @Test
    public void FindUserforeach() {
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(3);
        list.add(5);
        list.add(7);
        list.add(10);
        List<User> users = mapper.FindUserforeach(list);
        System.out.println("==============="+users);
    }


    /**
     * 分页查询
     */
    @Test
    public void FindUserpage() {
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        //使用分页插件 给sql语句加入分页功能
        //第一个参数 当前页面    第二个参数 显示3行
        PageHelper.startPage(3,3);
        List<User> userAll = mapper.FindUserPage();
        //将查询到了数据  转换到分页对象
        PageInfo<User> PageInfo = new PageInfo<>(userAll);

        int pageNum = PageInfo.getPageNum();
        int size = PageInfo.getSize();
        int pages = PageInfo.getPages();

        System.out.println("======当前页========"+pageNum);
        System.out.println("====== 当前页的数量========"+size);
        System.out.println("======总页数========"+pages);
        System.out.println("======PageInfo======="+PageInfo);

    }


    /**
     * 一级缓存测试, 二级缓存测试
     */
    @Test
    public void testCacheOneAndTwo() {
        Mapper mapper = MyBatisUtils.getMapper(Mapper.class);
        List<User> userAll = mapper.findUserAll();
        System.out.println("第一次"+userAll);
        MyBatisUtils.closeSession();
        mapper = MyBatisUtils.getMapper(Mapper.class);
        List<User> userAll1 = mapper.findUserAll();
        System.out.println("第二次"+userAll1);
    }
}
